package com.maibow.controller.system;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.maibow.controller.base.BaseController;
import com.maibow.entity.Page;
import com.maibow.service.admin.DictionaryService;
import com.maibow.service.system.AppFeedbackService;
import com.maibow.service.system.CommonService;
import com.maibow.util.DateUtil;
import com.maibow.util.PageData;

/**
 * 
 * 脉博网络后台管理系统
 * 包名：com.maibow.controller.system
 * 文件名：AppFeedbackController.java
 * 作者: 吴比(Wubi)
 * 时间：2015年6月14日-上午9:42:47
 * Copyright@ 2015湖南脉博网络科技有限公司-版权所有
 *
 */
@Controller
@RequestMapping(value = "/appFeedback")
public class AppFeedbackController extends BaseController {

    @Resource(name = "appFeedbackService")
    private AppFeedbackService appFeedbackService;

    @Resource(name = "dictionaryService")
    private DictionaryService dictionaryService;

    @Resource(name = "commonService")
    private CommonService commonService;

    /**
     * 列表
     */
    @RequestMapping(value = "/list")
    public ModelAndView list(Page page) {
        logBefore(logger, "列表意见反馈");
        ModelAndView mv = this.getModelAndView();
        PageData pageData = new PageData();
        try {
            pageData = this.getPageData();
            if (null != pageData.getString("lastStart")) {
                pageData.put("lastStart", DateUtil.getDateByStr(pageData.getString("lastStart")));
                pageData.put("lastStartShow", DateUtil.formatTimeDate((Date) pageData.get("lastStart")));
            }

            if (null != pageData.getString("lastEnd")) {
                pageData.put("lastEnd", DateUtil.getDateByStr(pageData.getString("lastEnd")));
                pageData.put("lastEndShow", DateUtil.formatTimeDate((Date) pageData.get("lastEnd")));
            }

            if (null != pageData.getString("deal_status") && !"".equals(pageData.getString("deal_status"))) {
                pageData.put("deal_status", Integer.valueOf(pageData.getString("deal_status")));
            } else {
                pageData.put("deal_status", null);
            }

            page.setPageData(pageData);
            List<PageData> varList = appFeedbackService.list(page); // 列出Order列表
            mv.setViewName("system/feedback/feedback_list");
            mv.addObject("varList", varList);
            mv.addObject("pageData", pageData);
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
        return mv;
    }

    /**
     * 去查看页面
     */
    @RequestMapping(value = "/goView")
    public ModelAndView goView() {
        logBefore(logger, "去查看意见反馈页面");
        ModelAndView mv = this.getModelAndView();
        PageData pageData = new PageData();
        pageData = this.getPageData();
        try {
            pageData.put("feedback_id", Integer.valueOf(pageData.get("feedback_id").toString()));
            pageData = appFeedbackService.findById(pageData); // 根据ID读取
            mv.setViewName("system/feedback/feedback_view");
            mv.addObject("pageData", pageData);
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
        return mv;
    }

    /**
     * 
     * goEditFeedback(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：ModelAndView
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @RequestMapping(value = "/goEditFeedback")
    public ModelAndView goEditFeedback() throws Exception {
        ModelAndView modelView = this.getModelAndView();
        PageData pageData = new PageData();
        pageData = this.getPageData();
        pageData.put("feedback_id", Integer.valueOf(pageData.get("feedback_id").toString()));
        pageData = appFeedbackService.findById(pageData); // 根据ID读取
        modelView.setViewName("system/feedback/feedback_edit");
        modelView.addObject("pageData", pageData);
        return modelView;
    }

    /**
     * 
     * editFeedback(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：ModelAndView
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @RequestMapping(value = "/editFeedback")
    public ModelAndView editFeedback() throws Exception {
        ModelAndView modelView = this.getModelAndView();
        PageData pageData = new PageData();
        pageData = this.getPageData();
        pageData.put("feedback_id", Integer.valueOf(pageData.get("feedback_id").toString()));
        pageData.put("deal_status", Integer.valueOf(pageData.get("deal_status").toString()));
        appFeedbackService.editFeedback(pageData);

        modelView.addObject("msg", "success");
        modelView.setViewName("save_result");
        return modelView;
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(format, true));
    }
}
